#include "databaseinterface.h"
#include <mutex>

#define EXEC_ASYNC_QUERY(sqlStr) DatabaseQueue::instance()->execAsyncSql(sqlStr);

#define EXEC_QUERY(sqlStr) QSqlQuery query= DatabaseQueue::instance()->execSql(sqlStr);

static std::once_flag               init_flag;
static DatabaseInterface*           m_DatabaseInterface = nullptr;

DatabaseInterface::DatabaseInterface()
{
    m_persondata = new Person();
    m_local = new Login();
}

DatabaseInterface *DatabaseInterface::Insterce()
{
    std::call_once(init_flag,[&](){
        m_DatabaseInterface = new DatabaseInterface();
    });
    return m_DatabaseInterface;
}

void DatabaseInterface::log(QString userid, QString pwd)
{
    QString sqlStr = QString("SELECT user_pawd from user where user_id = %1").arg(userid);
    EXEC_QUERY(sqlStr);
    while(query.next()){
        if(query.value("user_pawd").toString() == pwd){
            passwordpair = true;
            return ;
        }
    }
    passwordpair = false;
}

bool DatabaseInterface::getPasswordPair()
{
    return passwordpair;
}

void DatabaseInterface::persondata(QString userid)
{
    QString sqlStr = QString("SELECT * from user where user_id = %1").arg(userid);
    EXEC_QUERY(sqlStr);
    Person m_per;
    while(query.next()){
        m_per.Id = query.value("user_id").toInt();
        m_per.Name = query.value("user_name").toString();
        m_per.Sex = query.value("user_sex").toString();
        m_per.Phone = query.value("user_phone").toString();
        m_per.Email = query.value("user_email").toString();
        m_per.Major = query.value("user_class").toString();
        m_per.Time = query.value("user_time").toString();
        m_per.Pwd = query.value("user_pawd").toString();
    }
    *m_persondata = m_per;
}

Person *DatabaseInterface::getpersondata()
{
    return m_persondata;
}

void DatabaseInterface::setPersondata(QString id,QString phone, QString email)
{
    QString sqlStr = QString("UPDATE user SET user_phone = \"%1\","
                             "user_email = \"%2\" where user_id = %3").arg(phone).arg(email).arg(id);
    EXEC_ASYNC_QUERY(sqlStr);
    persondata(id);
}

void DatabaseInterface::setLocal(QString id, QString pwd)
{
    QString sqlStr = QString("delete from locallogin;");
    EXEC_ASYNC_QUERY(sqlStr);
    if(pwd=="")
        sqlStr = QString("insert into locallogin (localid) values(%1)").arg(id);
    else
        sqlStr = QString("insert into locallogin (localid,localpwd) values(%1,%2)").arg(id).arg(pwd);
    EXEC_ASYNC_QUERY(sqlStr);
}

void DatabaseInterface::seachLocal()
{
    QString sqlStr = QString("SELECT localid,localpwd from locallogin");
    EXEC_QUERY(sqlStr);
    Login local;
    while(query.next()){
        local.id = query.value("localid").toString();
        local.pwd = query.value("localpwd").toString();
    }
    *m_local = local;
}

Login *DatabaseInterface::getLocal()
{
    return m_local;
}

void DatabaseInterface::setChangedPasswod(QString pwd,QString id)
{
    QString sqlStr = QString("UPDATE user SET user_pawd = %1 where user_id = %2").arg(pwd).arg(id);
    EXEC_ASYNC_QUERY(sqlStr);
}

QVector<Curriculum> DatabaseInterface::setCurriculum()
{
    QString sqlStr = QString("SELECT * from curriculum");
    EXEC_QUERY(sqlStr);
    Curriculum   m_cur;
    m_curriculum.clear();
    while(query.next()){
        m_cur.cur_id = query.value("cur_id").toInt();
        m_cur.cur_name = query.value("cur_name").toString();
        m_cur.cur_school = query.value("cur_school").toString();
        m_cur.cur_numbers = query.value("cur_numbers").toInt();
        m_cur.cur_intro = query.value("cur_intro").toString();
        m_cur.cur_teacher = query.value("cur_teacher").toString();
        m_cur.cur_time = query.value("cur_time").toString();
//        qDebug()<<m_cur.cur_id<<"----------------"<<m_cur.cur_intro;
        m_curriculum.push_back(m_cur);
    }
    return m_curriculum;
}

QVector<Curriculum> DatabaseInterface::getCurriculum()
{
    return m_curriculum;
}

void DatabaseInterface::joinCurriculum(int user_id, int cur_id)
{
    QString sqlStr = QString("insert into mycurriculum (user_id,cur_id) values(%1,%2)").arg(user_id).arg(cur_id);
    EXEC_ASYNC_QUERY(sqlStr);
}

void DatabaseInterface::isSelectCur(int user_id, int cur_id)
{
    QString sqlStr = QString("select user_id from mycurriculum where cur_id = %1").arg(cur_id);
    EXEC_QUERY(sqlStr);
    while(query.next()){
        if(user_id == query.value("user_id").toInt()){
            is_SelectCur = true;
            return ;
        }
    }
    is_SelectCur = false;
}

bool DatabaseInterface::getIsSelectCur()
{
    return is_SelectCur;
}

void DatabaseInterface::setMyCurriculum(int user_id)
{
    QVector<int> cur_id;
    m_mycurriculum.clear();
    QString sqlStr = QString("select cur_id from mycurriculum where user_id = %1").arg(user_id);
    EXEC_QUERY(sqlStr);
    while (query.next()) {
        int id = query.value("cur_id").toInt();
        cur_id.push_back(id);
    }
    for(int i=0;i<cur_id.size();i++){
        QString sqlStr1 = QString("select * from curriculum where cur_id = %1").arg(cur_id[i]);
        EXEC_QUERY(sqlStr1);
        Curriculum   m_cur;
        while (query.next()) {
            m_cur.cur_id = query.value("cur_id").toInt();
            m_cur.cur_name = query.value("cur_name").toString();
            m_cur.cur_school = query.value("cur_school").toString();
            m_cur.cur_numbers = query.value("cur_numbers").toInt();
            m_cur.cur_intro = query.value("cur_intro").toString();
            m_cur.cur_teacher = query.value("cur_teacher").toString();
            m_cur.cur_time = query.value("cur_time").toString();
            m_mycurriculum.push_back(m_cur);
        }
    }
}

QVector<Curriculum> DatabaseInterface::getMyCurriculum()
{
    return m_mycurriculum;
}

